package com.example.bishe.config;

import com.alibaba.fastjson.JSONObject;
import com.example.bishe.entity.Tag;
import com.example.bishe.toolclass.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author yws
 * @Date 2022/3/6 15:46
 */
@Configuration
@EnableWebSecurity
@EnableGlobalAuthentication
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    BiaoQIan biaoQIan;

    //授权部分
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        String []student = {"/WebLogin","/user/student","/bj/selectbj","/bzks/updatebzks","/bzks/selectbzks","/jg/selectAllJg","/yx/selectyx","/zy/selectzy"};//学生
        String []teacher = {"/WebLogin","/user/teacher","/bj","/bzks","/dw/selectdw","/jg/selectAllJg","/jzg/selectjzg","/yx","/zy"};
        http.authorizeRequests()
                .antMatchers("/*").permitAll()
        /*.antMatchers("/*").hasRole("管理员")
        .antMatchers(student).hasRole("学生")
        .antMatchers(teacher).hasRole("教师")*/;
        //http.formLogin();

        http.csrf().disable();

        /**
         * 注销功能：前端请求/logout请求注销，
         * http.logout().logoutUrl("首页地址");//就可以跳转到首页
         */
        //http.logout().logoutUrl("/index");
    }

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /**
         * 根据标签，获得标签下的所有人
         */

        List<Tag> list = biaoQIan.biaoQian();//获取到所有的标签信息
        //System.out.println(list);
        Map<String,List<TagUser>> tagUsers = biaoQIan.gitTagUser(list);
        Set<String> set = new HashSet();
        set = tagUsers.keySet();
        for (String str:set) {//输出所有的标签和对应标签下的人员
            for (TagUser x:tagUsers.get(str)){
                //System.out.println("标签名称"+str+x.getUserid() + " " + x.getName());
                //根据企业微信中的对应的标签，然后给响应的人员对应的权限
                auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                        .withUser(x.getUserid()).password(new BCryptPasswordEncoder().encode("123456")).roles(str);
            }
        }

    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
